{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Client\n",
    "\n",
    "Client code interacting with a server that implements: \n",
    "\n",
    "* custom streaming for an agent\n",
    "* agent with user selected tools\n",
    "\n",
    "This agent does not have memory! See other examples in LangServe to see how to add memory.\n",
    "\n",
    "**ATTENTION** We made the agent stream strings as an output. This is almost certainly not what you would want for your application. Feel free to adapt to return more structured output; however, keep in mind that likely the client can just use `astream_events`!\n",
    "\n",
    "See relevant documentation about agents:\n",
    "\n",
    "* Creating a custom agent: https://python.langchain.com/docs/modules/agents/how_to/custom_agent\n",
    "* Streaming with agents: https://python.langchain.com/docs/modules/agents/how_to/streaming#custom-streaming-with-events\n",
    "* General streaming documentation: https://python.langchain.com/docs/expression_language/streaming"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can interact with this via API directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word = \"audioeeeeeeeeeee\"\n",
    "len(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Starting agent: agent with input: {'input': 'what is the length of the word audioeeeeeeeeeee?'}\n",
      "The length of the word \"audioeeeeeeeeeee\" is 15 characters.\n",
      "Done agent: agent with output: The length of the word \"audioeeeeeeeeeee\" is 15 characters.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "inputs = {\"input\": {\"input\": f\"what is the length of the word {word}?\", \"chat_history\": [], \"tools\": []}}\n",
    "response = requests.post(\"http://localhost:8000/invoke\", json=inputs)\n",
    "\n",
    "print(response.json()['output'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's provide it with a tool to test that tool selection works"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Starting agent: agent with input: {'input': 'what is the length of the word audioeeeeeeeeeee?'}\n",
      "\n",
      "Starting tool: word_length with inputs: {'word': 'audioeeeeeeeeeee'}\n",
      "\n",
      "Done tool: word_length with output: 16\n",
      "The length of the word \"audioeeeeeeeeeee\" is 16.\n",
      "Done agent: agent with output: The length of the word \"audioeeeeeeeeeee\" is 16.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "inputs = {\"input\": {\"input\": f\"what is the length of the word {word}?\", \"chat_history\": [], \"tools\": [\"word_length\", \"favorite_animal\"]}}\n",
    "response = requests.post(\"http://localhost:8000/invoke\", json=inputs)\n",
    "\n",
    "print(response.json()['output'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also interact with this via the RemoteRunnable interface (to use in other chains)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langserve import RemoteRunnable\n",
    "\n",
    "remote_runnable = RemoteRunnable(\"http://localhost:8000/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remote runnable has the same interface as local runnables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stream\n",
    "\n",
    "Streaming output from a **CUSTOM STREAMING** implementation that streams string representations of intermediate steps. Please see server side implementation for details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "|Starting agent: agent with input: {'input': 'What is eugenes favorite animal?'}|\n",
      "|I|'m| sorry|,| but| I| don|'t| have| access| to| personal| information| about| individuals| unless| it| has| been| shared| with| me| in| the| course| of| our| conversation|.|\n",
      "|Done agent: agent with output: I'm sorry, but I don't have access to personal information about individuals unless it has been shared with me in the course of our conversation.|\n",
      "|"
     ]
    }
   ],
   "source": [
    "async for chunk in remote_runnable.astream({\"input\": \"What is eugenes favorite animal?\", \"tools\": [\"word_length\"]}):\n",
    "    print(chunk, end='|', flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "|Starting agent: agent with input: {'input': 'What is eugenes favorite animal?'}|\n",
      "|\n",
      "|Starting tool: favorite_animal with inputs: {'name': 'Eugene'}|\n",
      "|\n",
      "|Done tool: favorite_animal with output: cat|\n",
      "|E|ug|ene|'s| favorite| animal| is| a| cat|.|\n",
      "|Done agent: agent with output: Eugene's favorite animal is a cat.|\n",
      "|"
     ]
    }
   ],
   "source": [
    "async for chunk in remote_runnable.astream({\"input\": \"What is eugenes favorite animal?\", \"tools\": [\"word_length\", \"favorite_animal\"]}):\n",
    "    print(chunk, end='|', flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
